package com.han.gift.controller;





import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.han.gift.common.util.AjaxResult;
import com.han.gift.common.util.PageList;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.han.gift.entity.TenantType;
import com.han.gift.query.TenantTypeQuery;
import com.han.gift.service.TenantTypeService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("tenantType")
public class TenantTypeController{

    //@Autowired
    //public ${table.serviceName} tenantTypeService;

	/**
     * 服务对象
     */
    @Autowired
    private TenantTypeService tenantTypeService;


    /**
     * 保存和修改公用的
     * @param TenantType  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody TenantType tenantType){
        try {
            if( tenantType.getId()!=null)
                tenantTypeService.updateById(tenantType);
            else
                tenantTypeService.save(tenantType);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {

            tenantTypeService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMessage("删除对象失败！"+e.getMessage());
        }
    }

    //获取用户
    @GetMapping("/{id}")
    public AjaxResult get(@PathVariable("id")Long id)
    {
        try {

            TenantType tenantType = tenantTypeService.getById(id);
            return AjaxResult.me().setResultObj(tenantType);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取一个失败！"+e.getMessage());
        }
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping
    public AjaxResult list(){

        try {
            List< TenantType> list = tenantTypeService.list(null);
            return AjaxResult.me().setResultObj(list);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("获取所有失败！"+e.getMessage());
        }
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */

    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public AjaxResult json(@RequestBody TenantTypeQuery query)
    {
        Page<TenantType> page = new Page<>(query.getPage(), query.getRows());

        String keyword = query.getKeyword();
        if(Strings.isNotBlank(keyword)){
            LambdaQueryWrapper<TenantType> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.like(TenantType::getDescription,keyword)
                    .or().like(TenantType::getName,keyword);
            page=tenantTypeService.page(page,queryWrapper);
            PageList pageList = new PageList<TenantType>(page.getTotal(),page.getRecords());
            return AjaxResult.me().setResultObj(pageList);
        }

        page = tenantTypeService.page(page);
        PageList pageList = new PageList<TenantType>(page.getTotal(),page.getRecords());
        return AjaxResult.me().setResultObj(pageList);
    }


    //批量删除
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){

        tenantTypeService.removeByIds(ids);

        return AjaxResult.me();

    }

}

